<template>
  <div>
    <el-dialog
      :title="info.isadd ? '分类添加' : '分类编辑'"
      :visible.sync="info.isshow"
      @closed="closed"
    >
      <el-form :model="user"
        >{{ user }}
        <el-form-item label="上级分类" label-width="120px">
          <el-select v-model="user.pid" placeholder="请选择">
            <el-option label="顶级分类" :value="0"></el-option>
            <el-option v-for="item in list" :key="item.id" :label="item.catename" :value="item.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="分类名称" label-width="120px">
          <el-input v-model="user.catename" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="图片" label-width="120px">
          <!-- 图片 -->
          <div class="my-upload">
            <h3>+</h3>
            <input type="file" @change="changimg" v-if="info.isshow" >
            <img :src="imgUrl" alt="" v-if="imgUrl" />
          </div>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="user.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="add" v-if="info.isadd"
          >确 定添加</el-button
        >
        <el-button type="primary" @click="updata" v-else>确定修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { IndexRoutes } from "../../../router";
import {

  reqroleedit,
  reqroleinfo,
  reqmenulist,
  reqcateadd,
  reqcateinfo,
  reqcateedit,
} from "../../../http/api";
import { errorAlert, successAlert } from "../../../utils//alert";
import path from "path";
export default {
  props: ["info"],
  data() {
    return {
      // 菜单列表出数值
      menuList: [],

      //   表单数据
      user:{
        pid: "",
        catename: "",
        img: "",
        status: 2,
      },
      imgUrl: "",
     
    };
  },
  computed: {
    ...mapGetters({  list:"cate/list" }),
  
  },
  methods: {
    ...mapActions({reqList:"cate/reqList"}),
    cancel() {
      this.info.isshow = false;
    },
    // 换图片
    changimg(e) {
      console.log(e);
      let file = e.target.files[0];
      // 文件大小
        if (file.size > 2 * 1024 * 1024) {
        errorAlert("文件大小不能超过2M");
        return;
    }
      let  imgArr =[".jpg",".png",".jpeg",".gif"];
      // 提取文件后缀名
        // let extname=file.name.slice(file.name.lastIndexOf("."))//js -2
      let extname= path.extname(file.name)
      if(!imgArr.includes(extname)){
        errorAlert("请上传正确的格式");
        return
      }
      // console.log(file);
      // 赋值img给user.img
      // 根据图片生成地址
       this.imgUrl =URL.createObjectURL(file)
      //  判断文件类型
      
      this.user.img = file;
    },

    // 清空user
    empty() {
      this.user = {
        pid: "",
        catename: "",
        img: "",
        status: 2,
      };
      this.imgUrl=""
      // this.$refs.input.files=null
      
    },
    // 添加数据
    add() {
    
      reqcateadd(this.user).then((res) => {
                
        if (res.data.code === 200) {
          successAlert(res.data.msg);
          // 清空·user
          console.log(res);
          this.empty();
          this.info.isshow = false;
          // 刷新列表
         this.reqList()
        }
      });
    },
    // changpia() {
    //   if (this.user.pid ===0) {
    //     this.user.type = 1;
    //   } else {
    //     this.user.type = 2;
    //   }
    // },
    getone(id) {
     
      reqcateinfo({ id: id }).then((res) => {
        console.log(res.data.code === 200);
        if (res.data.code == 200) {
          this.user = res.data.list;
          this.user.id = id;
          // 补个id
          
          // 处理图片+
          this.imgUrl=this.$pre+this.user.img
          
       
        }
      });
    },
    // 修改
    updata() {
    
      reqcateedit(this.user).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
         
         
          this.empty();
          this.info.isshow = false;
          // 刷新列表
          this.reqList()
        }
      });
    },
    closed() {
      // 若果是编辑的关闭 就执行empty
      // 不是添加
      if (!this.info.isadd) {
        this.empty();
      }
    },
  },
  mounted() {
    // 请求菜单列表数据
    // reqmenulist({ istree: true }).then((res) => {
    //   if (res.data.code == 200) {
    //     console.log(res);
    //     this.menuList = res.data.list;
    //   }
    // });
  },
};
</script>
<style lang="less" scoped>
@import "../../../less/index.less";
.my-upload {
  width: 150px;
  height: 150px;
  border: 1px dashed @primary;
  position: relative;

  h3 {
    width: 100%;
    height: 100%;
    text-align: center;
    line-height: 150px;
    font-size: @h1;
    font-weight: 400;
    color: @primary;
  }
  input {
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
    opacity: 0;
    z-index: 100;
    cursor: pointer;
  }
  img {
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
  }
}

/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>